﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.Mvc;
using System.Web.Providers.Entities;

namespace wne.code
{
    public class AuthenticateAttribute : AuthorizeAttribute
    {
        public bool Admin { get; set; }

        public AuthenticateAttribute()
        {
        }

        public AuthenticateAttribute(bool admin)
        {
            Admin = admin;
        }
        protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
        {
            if (httpContext.Request.Cookies["auth"] != null)
            {
                if (Admin)
                {
                    if (httpContext.Request.Cookies["admin"] != null)
                        return true;
                    else
                    {
                        return false;
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
        {
            filterContext.Result = new System.Web.Mvc.RedirectResult("/Account/LogOn?returnUrl=" + HttpContext.Current.Request.Url, false);
        }
    }
}